package fsoperation;

import java.io.File;
import mudus.GlobalSettings;
import mudus.UserInfo;

public class FolderUtil {
    
    //membuat direktori baru dengan nama dirname pada path
    public static boolean mkdir(UserInfo userInfo, String path, String dirname) {
        String fullpath = GlobalSettings.REPO_PATH + "/" + userInfo.getUsername() + "/" + dirname;
        File file = new File(fullpath);
        System.out.println(fullpath);
        return file.mkdir();
    }
    
    public static boolean rmdir(UserInfo userInfo, String path) {        
        String fullPath = GlobalSettings.REPO_PATH + "/" + userInfo.getUsername() + "/" + path;
        File dir = new File(fullPath);
        return rmdir(userInfo, dir);
    }
    
    public static boolean rmdir(UserInfo userInfo, File dir) {
        if(dir.isDirectory()) {
            String[] childs = dir.list();
            for(int i=0;i<childs.length;i++) {
                rmdir(userInfo, new File(dir, childs[i]));                
            }
            return dir.delete();
        }     
        return false;
    }
    
    public static void main(String[] args) {
        UserInfo userInfo = new UserInfo(1, "hilmanbeyri");
        System.out.println("hasil mkdir: " + mkdir(userInfo, "", "coba tes"));
        System.out.println("hasil rmdir: " + rmdir(userInfo, "coba tes"));
    }
    
}
